package com.nhjf.service.impl.coursesPlayback;

import com.nhjf.dao.coursesPlayback.CoursesPlaybackDao;
import com.nhjf.dao.teachers.CoursesDao;
import com.nhjf.dao.teachers.TeachersDao;
import com.nhjf.model.coursesPlayback.CoursesPlayback;
import com.nhjf.model.teachers.Courses;
import com.nhjf.model.teachers.Teacher;
import com.nhjf.pojo.dto.result.ServiceResultDto;
import com.nhjf.pojo.qo.coursesPlayback.CoursesPlaybackQo;
import com.nhjf.pojo.qo.teachers.CoursesQo;
import com.nhjf.pojo.qo.teachers.TeachersQo;
import com.nhjf.service.coursesPlayback.CoursesPlaybackService;
import com.nhjf.service.impl.BaseServiceImpl;
import com.nhjf.service.teachers.CoursesService;

import java.io.File;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service("coursesPlaybackService")
public class CoursesPlaybackServiceImpl extends BaseServiceImpl<CoursesPlayback,CoursesPlaybackQo,CoursesPlaybackDao> implements CoursesPlaybackService {
    @Autowired
    private CoursesPlaybackDao dao;
//    @Autowired
//    private  CoursesService CoursesService;
    
    @Autowired
    private TeachersDao teacherDao;

    @Override
    protected CoursesPlaybackDao getDao() {
        return dao;
    }

    //课程查询、修改、添加
    @Override
    public ServiceResultDto findadd(CoursesPlaybackQo playbackQo) {

        ServiceResultDto resultDto = new ServiceResultDto();
        CoursesPlayback Addtocourses=new CoursesPlayback();
        if(playbackQo==null){
            resultDto.setSuccess(false);
            resultDto.setResultMessage("非法操作");
            return resultDto;
        }

        //课程标题
        if(playbackQo.getCoursetitle()!=null){
        	Addtocourses.setCoursetitle(playbackQo.getCoursetitle());
        }else {
        	resultDto.setResultMessage("课程标题为空");
            resultDto.setSuccess(false);
            return resultDto;
        }

        //二级课程标题
        if(playbackQo.getCoursetitleTwo()!=null){
            Addtocourses.setCoursetitleTwo(playbackQo.getCoursetitleTwo());
        }
        //封面
        if(playbackQo.getImge()!=null){
            Addtocourses.setImge(playbackQo.getImge());
        }else{
        	resultDto.setResultMessage("封面为空");
            resultDto.setSuccess(false);
            return resultDto;
        }
        //课程视频
        if(playbackQo.getVideo()!=null){
            Addtocourses.setVideo(playbackQo.getVideo());
        }else{
        	resultDto.setResultMessage("视频为空");
            resultDto.setSuccess(false);
            return resultDto;
        }
        //是否付费
        if(playbackQo.getIsPay()==1||playbackQo.getIsPay()==2){
            Addtocourses.setIsPay(playbackQo.getIsPay());
        }
        
        //讲师
        if(playbackQo.getTeacher()!=null){
            Addtocourses.setTeacher(playbackQo.getTeacher());
        }
        //一级分类
        if(playbackQo.getCourseTypeOne()!=null){
            Addtocourses.setCourseTypeOneId(playbackQo.getCourseTypeOne());
        }
        //二级分类
        if(playbackQo.getCourseTypeTwo()!=null){
            Addtocourses.setCourseTypeTwoId(playbackQo.getCourseTypeTwo());
        }
        //课程共享
        if(playbackQo.getCourseShare()==1||playbackQo.getCourseShare()==2||playbackQo.getCourseShare()==3){
            Addtocourses.setCourseShare(playbackQo.getCourseShare());
        }
        //课程简介
        if(playbackQo.getCourseintroduction()!=null){
            Addtocourses.setCourseintroduction(playbackQo.getCourseintroduction());
        }else{
        	resultDto.setResultMessage("课程简介为空");
            resultDto.setSuccess(false);
            return resultDto;
        }
        if(StringUtils.isNotBlank(playbackQo.getViewTime())) {
        	 Addtocourses.setViewTime(playbackQo.getViewTime());
        }
        if(playbackQo.getUploader()!=null) {
        	TeachersQo teachersQo=new TeachersQo();
        	
        	teachersQo.setSeachOrganization(true);
        	teachersQo.setSeachUser(true);
        	teachersQo.setTeacherId(playbackQo.getUploader());
        	Teacher teacher=teacherDao.queryUnique(teachersQo);
        	teacher.setCoursesNum(teacher.getCoursesNum()+1);
        	Addtocourses.setUploader(teacher);
        	teacherDao.update(teacher);
        	Addtocourses.setUploader(teacher);
        }else {
        	resultDto.setResultMessage("上传人不能为空！");
            resultDto.setSuccess(false);
            return resultDto;
        }
        Addtocourses.setReleasetime(new Date());
        dao.save(Addtocourses);
        resultDto.setSuccess(true);
        return resultDto;
    }

    //修改
    @Override
    public ServiceResultDto findpmodify(CoursesPlaybackQo playbackQo) {
    	 ServiceResultDto resultDto = new ServiceResultDto();
    	 CoursesPlaybackQo cou =new CoursesPlaybackQo();
    	 cou.setCoursesPlaybackId(playbackQo.getCoursesPlaybackId());
    	 CoursesPlayback addtocourses=dao.queryUnique(cou);
         
         if(playbackQo==null){
             resultDto.setSuccess(false);
             resultDto.setResultMessage("非法操作");
             return resultDto;
         }
         //课程标题
         if(playbackQo.getCoursetitle()!=null){
        	 addtocourses.setCoursetitle(playbackQo.getCoursetitle());
         }else {
         	resultDto.setResultMessage("课程标题为空");
             resultDto.setSuccess(false);
             return resultDto;
         }
         //二级课程标题
         if(playbackQo.getCoursetitleTwo()!=null){
        	 addtocourses.setCoursetitleTwo(playbackQo.getCoursetitleTwo());
         }
         //封面
         if(playbackQo.getImge()!=null){
        	 addtocourses.setImge(playbackQo.getImge());
         }else{
//         	resultDto.setResultMessage("封面为空");
//             resultDto.setSuccess(false);
//             return resultDto;
         }
         if(StringUtils.isNotBlank(playbackQo.getViewTime())) {
        	 addtocourses.setViewTime(playbackQo.getViewTime());
         }
         //课程视频
         if(playbackQo.getVideo()!=null){
        	 addtocourses.setVideo(playbackQo.getVideo());
         }
         //是否付费
         if(playbackQo.getIsPay()==1){
        	 addtocourses.setIsPay(playbackQo.getIsPay());
         }else if(playbackQo.getIsPay()==2) {
        	 addtocourses.setIsPay(playbackQo.getIsPay());
        	 if(playbackQo.getPay()==null) {
                 resultDto.setSuccess(false);
                 resultDto.setResultMessage("付费课程 必须添费用！");
                 return resultDto;
        	 }else {
        		 addtocourses.setPay(playbackQo.getPay());
        	 }
         }
         
         //讲师
         if(playbackQo.getTeacher()!=null){
        	 addtocourses.setTeacher(playbackQo.getTeacher());
         }
         //一级分类
         if(playbackQo.getCourseTypeOne()!=null){
        	 addtocourses.setCourseTypeTwoId(playbackQo.getCourseTypeOne());
         }
         //二级分类
         if(playbackQo.getCourseTypeTwo()!=null){
        	 addtocourses.setCourseTypeTwoId(playbackQo.getCourseTypeTwo());
         }
         //课程共享
         if(playbackQo.getCourseShare()==1||playbackQo.getCourseShare()==2||playbackQo.getCourseShare()==3){
        	 addtocourses.setCourseShare(playbackQo.getCourseShare());
         }
         //课程简介
         if(playbackQo.getCourseintroduction()!=null){
        	 addtocourses.setCourseintroduction(playbackQo.getCourseintroduction());
         }
//         Addtocourses.set
         dao.update(addtocourses);
         resultDto.setSuccess(true);
         return resultDto;
    }
}
